#pip install plotly==5.11.0
#pip install nbconvert
#pip install nbconvert[webpdf]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import os
import csv
import xlrd
import seaborn as sns
import plotly.graph_objects as go
from ipywidgets import widgets
import plotly.express as px
from plotly.subplots import make_subplots
import scipy
import ipywidgets as widgets
filepath = 'C:\\Users\\areid\\OneDrive\\ESPM Ph.D\\Research\\yield gap &pop proj Research\\Data\\MAIN Data sets\\'
Self_Sufficiency_Climate = pd.read_csv(filepath +'FINAL Self Sufficiency Ratios with Climate Change.csv')
Current_Self_Sufficiency = pd.read_csv(filepath +'FINAL Self-Sufficiency Ratios for year 2000 data (curent).csv')
Preparing Dataframes for figures
Self_Sufficiency_Climate = Self_Sufficiency_Climate.drop(columns=['Unnamed: 0'])
Self_Sufficiency_Climate.head()
| FAO Country Code | UN Country Code | ISO | MagPie Region Code | SSP Region | Country | SSP Scenario | Population Scenario | RCP | Year | Total kcal demand | Sum of yield CC 4 major crops | Sum of 18 crops w/o 4 major crops | TOTAL Production with Climate Change (all 22 crops) | Self-Sufficiency Ratio | Reclassified Self-Sufficiency Ratios | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Low | 2.6 | 2030 | 4.371143e+13 | 1.677106e+13 | 5.442117e+12 | 2.221318e+13 | 0.508178 | 0.508178 |
| 1 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Low | 4.5 | 2030 | 4.371143e+13 | 1.698837e+13 | 5.442117e+12 | 2.243049e+13 | 0.513149 | 0.513149 |
| 2 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Low | 6.0 | 2030 | 4.371143e+13 | 1.781240e+13 | 5.442117e+12 | 2.325451e+13 | 0.532001 | 0.532001 |
| 3 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Low | 8.5 | 2030 | 4.371143e+13 | 1.725274e+13 | 5.442117e+12 | 2.269486e+13 | 0.519197 | 0.519197 |
| 4 | 16.0 | 50.0 | BGD | SAS | ASIA | Bangladesh | SSP1-Baseline | Low | 2.6 | 2030 | 1.478124e+14 | 1.364770e+14 | 8.430541e+12 | 1.449075e+14 | 0.980347 | 0.980347 |
Current_Self_Sufficiency = Current_Self_Sufficiency.drop(columns = ['Unnamed: 0'])
Current_Self_Sufficiency = Current_Self_Sufficiency.rename(columns ={'Reclassified Self-Sufficiency Ratios (year 2000)':'Reclassified Self-Sufficiency Ratios'})
Current_Self_Sufficiency['Year'] = Current_Self_Sufficiency['Year'].astype(int)
Current_Self_Sufficiency.head()
| UN Country code | FAO Country Code | ISO | MagPie Region Code | SSP Region | Country | Year | Historical 2000 Pop | Total kcal production of 22 crops year 2000 | Food supply (kcal/capita/day) | Food supply for year 2000 (kcal/capita/YEAR) | kcal consumption per country in 2000 (Food Supply * Population) | Self-Sufficiency Ratios (year 2000) | Reclassified Self-Sufficiency Ratios | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 231 | 238.0 | ETH | AFR | MAF | Ethiopia | 2000 | 66224804.0 | 1.949408e+13 | 1786.0 | 651890.0 | 4.317129e+13 | 0.451552 | 0.451552 |
| 1 | 404 | 114.0 | KEN | AFR | MAF | Kenya | 2000 | 31964557.0 | 1.382456e+13 | 2049.0 | 747885.0 | 2.390581e+13 | 0.578293 | 0.578293 |
| 2 | 450 | 129.0 | MDG | AFR | MAF | Madagascar | 2000 | 15766806.0 | 6.431309e+12 | 2006.0 | 732190.0 | 1.154430e+13 | 0.557098 | 0.557098 |
| 3 | 454 | 130.0 | MWI | AFR | MAF | Malawi | 2000 | 11148758.0 | 4.282963e+12 | 2204.0 | 804460.0 | 8.968730e+12 | 0.477544 | 0.477544 |
| 4 | 480 | 137.0 | MUS | AFR | MAF | Mauritius | 2000 | 1185145.0 | 1.517476e+12 | 2904.0 | 1059960.0 | 1.256206e+12 | 1.207983 | 1.207983 |
Chloropleth Map for Self-Sufficiency Ratios in Year 2000 (Historical)
fig_1 = go.Figure(data=go.Choropleth(
locations = Current_Self_Sufficiency['ISO'],
z = Current_Self_Sufficiency["Reclassified Self-Sufficiency Ratios"],
text = Current_Self_Sufficiency['Country'],
colorbar=dict(
title="Self-Sufficiency Ratio",
titleside="top",
tickmode="array",
tickvals=[0, 0.5, 1, 1.5, 2.1],
ticktext=["0","0.5", "1","1.5", ">2"],
ticks="outside"
),
colorscale ='PRGn',
autocolorscale=False,
reversescale=False,
marker_line_color='darkgray',
marker_line_width=0.5,
colorbar_title = 'Self-Sufficiency Ratio',
))
fig_1.update_layout(
title_text='Self-Sufficiency Ratios in Year 2000 (Historical)',
geo=dict(
showframe=True,
showcoastlines=False,
projection_type='equirectangular'
)
)
fig_1.show()
Test Chloropleth Map for Self-Sufficiency Ratios in 2100 under RCP 8.5, SSP 3, Population Med
self_suff_rcp8_5_yr2100_ssp3_popMed = Self_Sufficiency_Climate.loc[(Self_Sufficiency_Climate['RCP']==8.5) & (Self_Sufficiency_Climate['Year']== 2100) & (Self_Sufficiency_Climate['SSP Scenario']== 'SSP3-Baseline')& (Self_Sufficiency_Climate['Population Scenario']== 'Med')]
fig_2 = go.Figure(data=go.Choropleth(
locations = self_suff_rcp8_5_yr2100_ssp3_popMed['ISO'],
z = self_suff_rcp8_5_yr2100_ssp3_popMed['Reclassified Self-Sufficiency Ratios'],
text = self_suff_rcp8_5_yr2100_ssp3_popMed['Country'],
colorscale = 'RdYlGn',
autocolorscale=False,
reversescale=False,
marker_line_color='darkgray',
marker_line_width=0.5,
colorbar_title = 'Self-Sufficiency Ratio',
))
fig_2.update_layout(
title_text='Self-Sufficiency Ratios in 2100 under RCP 8.5, SSP 3, Pop Med',
geo=dict(
showframe=True,
showcoastlines=False,
projection_type='equirectangular'
)
)
# annotations = [dict(
# x=0.55,
# y=0.1,
# xref='paper',
# yref='paper',
# text='Source: <a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">\
# CIA World Factbook</a>',
# showarrow = False
fig_2.show()
#conda install -c plotly plotly-orca psutil requests
import os
if not os.path.exists("images"):
os.mkdir("images")
#fig_2.write_image("images/fig1.jpeg")
Widget for dataframes with data based on projection year
ALL ='ALL'
dropdown_year = widgets.Dropdown(options = [2030, 2050, 2080, 2100])
output_year = widgets.Output()
def dropdown_year_eventhandler(change):
output_year.clear_output()
with output_year:
display(Self_Sufficiency_Climate.loc[Self_Sufficiency_Climate['Year'] == change.new])
dropdown_year.observe(dropdown_year_eventhandler, names='value')
display(dropdown_year)
display(output_year)
Dropdown(options=(2030, 2050, 2080, 2100), value=2030)
Output()
Final widget which produces the various chloropleth maps under the different scenario combinations (Year, RCP, SSP, and United Nations Population Variant). In this widget you can choose to display the chloropleth map or explore the filtered dataset under the specified scenario combinations.
output = widgets.Output()
plot_output = widgets.Output()
dropdown_year = widgets.Dropdown(options = [2000, 2030, 2050, 2080, 2100], description = 'Year:')
dropdown_RCP = widgets.Dropdown(options = [2.6, 4.5, 6.0, 8.5], description = 'RCP:')
dropdown_SSP = widgets.Dropdown(options = ['SSP1-Baseline', 'SSP2-Baseline', 'SSP3-Baseline'], description = 'SSP:')
dropdown_Population = widgets.Dropdown(options = ['Low', 'Med', 'High'], description = 'Population:')
def common_filtering(year, rcp, ssp, pop):
output.clear_output()
plot_output.clear_output()
if (year == 2000):
common_filter = Current_Self_Sufficiency
else:
common_filter = Self_Sufficiency_Climate.loc[(Self_Sufficiency_Climate['Year'] == year) &
(Self_Sufficiency_Climate['RCP'] == rcp) & (Self_Sufficiency_Climate['SSP Scenario'] == ssp) & (Self_Sufficiency_Climate['Population Scenario'] == pop)]
with output:
display(common_filter)
with plot_output:
if (year == 2000):
fig = go.Figure(data=go.Choropleth(
locations = common_filter['ISO'],
z = common_filter['Reclassified Self-Sufficiency Ratios'],
text = common_filter['Country'],
colorscale = 'RdYlGn',
autocolorscale=False,
reversescale=False,
marker_line_color='darkgray',
marker_line_width=0.5,
colorbar_title = 'Self-Sufficiency Ratio',
))
fig.update_layout(
title_text='Self-Sufficiency Ratios in year 2000',
geo=dict(
showframe=True,
showcoastlines=False,
projection_type='equirectangular'
)
)
else:
fig = go.Figure(data=go.Choropleth(
locations = common_filter['ISO'],
z = common_filter['Reclassified Self-Sufficiency Ratios'],
text = common_filter['Country'],
colorscale = 'RdYlGn',
autocolorscale=False,
reversescale=False,
marker_line_color='darkgray',
marker_line_width=0.5,
colorbar_title = 'Self-Sufficiency Ratio',
))
fig.update_layout(
title_text='Self-Sufficiency Ratios in '+ str(year) + ' under RCP ' + str(rcp) + ', '+ str(ssp) +', and ' + str(pop) + ' UN Population Variant',
geo=dict(
showframe=True,
showcoastlines=False,
projection_type='equirectangular'
)
)
fig.show()
def dropdown_year_eventhandler(change):
common_filtering(change.new, dropdown_RCP.value, dropdown_SSP.value, dropdown_Population.value)
def dropdown_RCP_eventhandler(change):
common_filtering(dropdown_year.value, change.new, dropdown_SSP.value, dropdown_Population.value)
def dropdown_SSP_eventhandler(change):
common_filtering(dropdown_year.value, dropdown_RCP.value, change.new, dropdown_Population.value)
def dropdown_Population_eventhandler(change):
common_filtering(dropdown_year.value, dropdown_RCP.value, dropdown_SSP.value, change.new)
dropdown_year.observe(dropdown_year_eventhandler, names='value')
dropdown_RCP.observe(dropdown_RCP_eventhandler, names='value')
dropdown_SSP.observe(dropdown_SSP_eventhandler, names='value')
dropdown_Population.observe(dropdown_Population_eventhandler, names = 'value')
# display(dropdown_year)
# display(dropdown_RCP)
# display(dropdown_SSP)
# display(dropdown_Population)
# display(output)
# display(plot_output)
item_layout = widgets.Layout(margin='0 0 50px 0')
input_widgets = widgets.HBox([dropdown_year, dropdown_RCP, dropdown_SSP, dropdown_Population],layout=item_layout)
tab = widgets.Tab([output, plot_output], layout=item_layout)
tab.set_title(0, 'Dataset Exploration')
tab.set_title(1, 'Choropleth Map')
dashboard = widgets.VBox([input_widgets, tab])
display(dashboard)
VBox(children=(HBox(children=(Dropdown(description='Year:', options=(2000, 2030, 2050, 2080, 2100), value=2000…
The code after this was preparing the dataframes to produce a box plot for a sensitivity analysis. However, I ended up creating a more complex figure in Tableau which was published in Beltran-Pena et al 2020.
Self_Sufficiency_Climate.head()
| FAO Country Code | UN Country Code | ISO | MagPie Region Code | SSP Region | Country | SSP Scenario | Population Scenario | RCP | Year | Total kcal demand | Sum of yield CC 4 major crops | Sum of 18 crops w/o 4 major crops | TOTAL Production with Climate Change (all 22 crops) | Self-Sufficiency Ratio | Reclassified Self-Sufficiency Ratios | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Low | 2.6 | 2030 | 4.371143e+13 | 1.677106e+13 | 5.442117e+12 | 2.221318e+13 | 0.508178 | 0.508178 |
| 1 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Low | 4.5 | 2030 | 4.371143e+13 | 1.698837e+13 | 5.442117e+12 | 2.243049e+13 | 0.513149 | 0.513149 |
| 2 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Low | 6.0 | 2030 | 4.371143e+13 | 1.781240e+13 | 5.442117e+12 | 2.325451e+13 | 0.532001 | 0.532001 |
| 3 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Low | 8.5 | 2030 | 4.371143e+13 | 1.725274e+13 | 5.442117e+12 | 2.269486e+13 | 0.519197 | 0.519197 |
| 4 | 16.0 | 50.0 | BGD | SAS | ASIA | Bangladesh | SSP1-Baseline | Low | 2.6 | 2030 | 1.478124e+14 | 1.364770e+14 | 8.430541e+12 | 1.449075e+14 | 0.980347 | 0.980347 |
Current_Self_Sufficiency.head()
| UN Country code | FAO Country Code | ISO | MagPie Region Code | SSP Region | Country | Year | Historical 2000 Pop | Total kcal production of 22 crops year 2000 | Food supply (kcal/capita/day) | Food supply for year 2000 (kcal/capita/YEAR) | kcal consumption per country in 2000 (Food Supply * Population) | Self-Sufficiency Ratios (year 2000) | Reclassified Self-Sufficiency Ratios | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 231 | 238.0 | ETH | AFR | MAF | Ethiopia | 2000 | 66224804.0 | 1.949408e+13 | 1786.0 | 651890.0 | 4.317129e+13 | 0.451552 | 0.451552 |
| 1 | 404 | 114.0 | KEN | AFR | MAF | Kenya | 2000 | 31964557.0 | 1.382456e+13 | 2049.0 | 747885.0 | 2.390581e+13 | 0.578293 | 0.578293 |
| 2 | 450 | 129.0 | MDG | AFR | MAF | Madagascar | 2000 | 15766806.0 | 6.431309e+12 | 2006.0 | 732190.0 | 1.154430e+13 | 0.557098 | 0.557098 |
| 3 | 454 | 130.0 | MWI | AFR | MAF | Malawi | 2000 | 11148758.0 | 4.282963e+12 | 2204.0 | 804460.0 | 8.968730e+12 | 0.477544 | 0.477544 |
| 4 | 480 | 137.0 | MUS | AFR | MAF | Mauritius | 2000 | 1185145.0 | 1.517476e+12 | 2904.0 | 1059960.0 | 1.256206e+12 | 1.207983 | 1.207983 |
Current_Self_Sufficiency = Current_Self_Sufficiency.rename(columns = {'UN Country code':'UN Country Code'})
Current_Self_Sufficiency = Current_Self_Sufficiency.rename(columns = {'Self-Sufficiency Ratios (year 2000)':'Self-Sufficiency Ratio'})
Current_future = Self_Sufficiency_Climate.append(Current_Self_Sufficiency, sort =False)
C:\Users\areid\AppData\Local\Temp\ipykernel_19280\1681316535.py:1: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
Current_future.columns
Index(['FAO Country Code', 'UN Country Code', 'ISO', 'MagPie Region Code',
'SSP Region', 'Country', 'SSP Scenario', 'Population Scenario', 'RCP',
'Year', 'Total kcal demand', 'Sum of yield CC 4 major crops',
'Sum of 18 crops w/o 4 major crops',
'TOTAL Production with Climate Change (all 22 crops)',
'Self-Sufficiency Ratio', 'Reclassified Self-Sufficiency Ratios',
'Historical 2000 Pop', 'Total kcal production of 22 crops year 2000',
'Food supply (kcal/capita/day)',
'Food supply for year 2000 (kcal/capita/YEAR)',
'kcal consumption per country in 2000 (Food Supply * Population)'],
dtype='object')
Current_future = Current_future[['FAO Country Code', 'UN Country Code', 'ISO', 'MagPie Region Code',
'SSP Region', 'Country', 'SSP Scenario', 'Population Scenario', 'RCP',
'Year', 'Self-Sufficiency Ratio', 'Reclassified Self-Sufficiency Ratios']]
SSP1 =Current_future.loc[(Current_future['Population Scenario']=='Med') & (Current_future['SSP Scenario']=='SSP1-Baseline')]
SSP1.head()
| FAO Country Code | UN Country Code | ISO | MagPie Region Code | SSP Region | Country | SSP Scenario | Population Scenario | RCP | Year | Self-Sufficiency Ratio | Reclassified Self-Sufficiency Ratios | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1936 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Med | 2.6 | 2030 | 0.496674 | 0.496674 |
| 1937 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Med | 4.5 | 2030 | 0.501533 | 0.501533 |
| 1938 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Med | 6.0 | 2030 | 0.519958 | 0.519958 |
| 1939 | 2.0 | 4.0 | AFG | SAS | ASIA | Afghanistan | SSP1-Baseline | Med | 8.5 | 2030 | 0.507444 | 0.507444 |
| 1940 | 16.0 | 50.0 | BGD | SAS | ASIA | Bangladesh | SSP1-Baseline | Med | 2.6 | 2030 | 0.955010 | 0.955010 |
table1 = pd.pivot_table(SSP1, values=['Self-Sufficiency Ratio'], index=['RCP', 'Year'],
aggfunc={'Self-Sufficiency Ratio': [min, max, np.mean]})
table1.reset_index()
| RCP | Year | Self-Sufficiency Ratio | |||
|---|---|---|---|---|---|
| max | mean | min | |||
| 0 | 2.6 | 2030 | 7.096696 | 1.409869 | 0.000032 |
| 1 | 2.6 | 2050 | 8.667064 | 1.440098 | 0.000028 |
| 2 | 2.6 | 2080 | 11.069850 | 1.584658 | 0.000026 |
| 3 | 2.6 | 2100 | 12.900592 | 1.733397 | 0.000025 |
| 4 | 4.5 | 2030 | 7.166408 | 1.413409 | 0.000032 |
| 5 | 4.5 | 2050 | 8.609940 | 1.408488 | 0.000028 |
| 6 | 4.5 | 2080 | 11.038063 | 1.591206 | 0.000026 |
| 7 | 4.5 | 2100 | 12.505311 | 1.712793 | 0.000025 |
| 8 | 6.0 | 2030 | 7.180286 | 1.417084 | 0.000032 |
| 9 | 6.0 | 2050 | 8.792020 | 1.458045 | 0.000028 |
| 10 | 6.0 | 2080 | 11.155593 | 1.604396 | 0.000026 |
| 11 | 6.0 | 2100 | 13.061541 | 1.768654 | 0.000025 |
| 12 | 8.5 | 2030 | 7.233112 | 1.439925 | 0.000031 |
| 13 | 8.5 | 2050 | 8.677833 | 1.437371 | 0.000028 |
| 14 | 8.5 | 2080 | 11.002759 | 1.579919 | 0.000025 |
| 15 | 8.5 | 2100 | 12.620570 | 1.723244 | 0.000024 |
table1 = table1['Self-Sufficiency Ratio'].reset_index()
#Current_future.to_csv(filepath + 'Current+Future SSR combined for figures.csv')
pd.read_csv(filepath +'Global_Self_Sufficiency.csv')
| Unnamed: 0 | Year | RCP | SSP Scenario | Population Scenario | Population | Population Not Self-Sufficient (in millions) | |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 2030 | 2.6 | SSP1-Baseline | High | 8.635244e+09 | -1804.776712 |
| 1 | 1 | 2030 | 2.6 | SSP1-Baseline | Low | 8.269339e+09 | -2170.682194 |
| 2 | 2 | 2030 | 2.6 | SSP1-Baseline | Medium | 8.452291e+09 | -1987.729398 |
| 3 | 3 | 2030 | 2.6 | SSP2-Baseline | High | 8.635244e+09 | -1317.046084 |
| 4 | 4 | 2030 | 2.6 | SSP2-Baseline | Low | 8.269339e+09 | -1682.951566 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 140 | 140 | 2100 | 8.5 | SSP2-Baseline | Medium | 1.076235e+10 | 861.604304 |
| 141 | 141 | 2100 | 8.5 | SSP3-Baseline | High | 1.544261e+10 | 9852.468930 |
| 142 | 142 | 2100 | 8.5 | SSP3-Baseline | Low | 7.247303e+09 | 1657.164588 |
| 143 | 143 | 2100 | 8.5 | SSP3-Baseline | Medium | 1.076472e+10 | 5174.580562 |
| 144 | 0 | 2000 | NaN | NaN | NaN | 5.939383e+09 | -2047.097527 |
145 rows × 7 columns